﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CPPEI.Coolzon.Core;
using CPPEI.Coolzon.Core.Data;
using CPPEI.Coolzon.Core.Repositories;

namespace CPPEI.Coolzon.ManagementCenter
{
    public partial class Privileges : System.Web.UI.Page
    {
        private static string userId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            using (DbContext context = new DbContext())
            {
                userId = Request.QueryString["userId"];
                IPrivilegeRepository privilegeRepository = RepositoryFactory.Create<IPrivilegeRepository>();
                PagedList<CPPEI.Coolzon.Core.Data.Privilege> privilegeList = privilegeRepository.GetPrivilegeList(context, 0, int.MaxValue);

                IAssignedPrivilegeRepository iAssignedPrivilegeRepository = RepositoryFactory.Create<IAssignedPrivilegeRepository>();
                PagedList<CPPEI.Coolzon.Core.Data.AssignedPrivilege> assignedPrivilegeList = iAssignedPrivilegeRepository.GetAssignedPrivilegeByObjectId(context, new Guid(userId));
                List<UserPrivileges> userPrivilegesList = new List<UserPrivileges>();
                foreach (Privilege privilege in privilegeList.DataList)
                {
                    UserPrivileges userPrivileges = new UserPrivileges();
                    userPrivileges.Id = privilege.Id;
                    userPrivileges.Code = privilege.Code;
                    userPrivileges.Name = privilege.Name;
                    userPrivileges.Type = privilege.Type;
                    userPrivileges.userId = new Guid(userId);
                    if (assignedPrivilegeList != null && assignedPrivilegeList.DataList != null && assignedPrivilegeList.DataList.Exists(tmp => tmp.PrivilegeId.Equals(privilege.Id)))
                    {
                        userPrivileges.IsHad = true;
                    }
                    else
                    {
                        userPrivileges.IsHad = false;
                    }
                    userPrivilegesList.Add(userPrivileges);
                }
                gvUserPrivilege.DataSource = userPrivilegesList;
                gvUserPrivilege.DataBind();
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                //获取当前已选中的方案列表
                List<AssignedPrivilege> privilegeIdList = new List<AssignedPrivilege>();
                PagedList<CPPEI.Coolzon.Core.Data.AssignedPrivilege> assignedPrivilegeList;
                foreach (GridViewRow row in gvUserPrivilege.Rows)
                {
                    if (((CheckBox)row.FindControl("chbPrivilege")).Checked)
                    {
                        AssignedPrivilege assignedPrivilege = new AssignedPrivilege();
                        assignedPrivilege.Id = Guid.NewGuid();
                        assignedPrivilege.ObjectType = "USER";
                        assignedPrivilege.ObjectData = "";
                        assignedPrivilege.ObjectId = new Guid(((Label)row.FindControl("lblUserId")).Text.Trim()); ;
                        assignedPrivilege.PrivilegeId = new Guid(((Label)row.FindControl("lblId")).Text.Trim());
                        privilegeIdList.Add(assignedPrivilege);
                    }
                }

                //根据UserId获取已分配的权限列表
                using (DbContext context1 = new DbContext())
                {
                    IAssignedPrivilegeRepository iAssignedPrivilegeRepository = RepositoryFactory.Create<IAssignedPrivilegeRepository>();
                    assignedPrivilegeList = iAssignedPrivilegeRepository.GetAssignedPrivilegeByObjectId(context1, new Guid(userId));
                }

                //比较已选中的方案列表与已分配的权限列表,获取添加列表
                if (privilegeIdList != null && privilegeIdList.Count > 0)
                {
                    IAssignedPrivilegeRepository iAssignedPrivilegeRepository = RepositoryFactory.Create<IAssignedPrivilegeRepository>();
                    if (assignedPrivilegeList != null && assignedPrivilegeList.DataList != null)
                    {
                        using (DbContext context = new DbContext())
                        {
                           
                            foreach (var oAssignedPrivilege in assignedPrivilegeList.DataList)
                            {
                                iAssignedPrivilegeRepository.RemoveAssignedPrivilege(context, oAssignedPrivilege);
                            }
                            foreach (var cheeckassignedPrivilege in privilegeIdList)
                            {
                                iAssignedPrivilegeRepository.CreateAssignedPrivilege(context, cheeckassignedPrivilege);
                            }
                        }
                    }
                    else
                    {
                        using (DbContext context = new DbContext())
                        {
                            foreach (var cheeckassignedPrivilege in privilegeIdList)
                            {
                                iAssignedPrivilegeRepository.CreateAssignedPrivilege(context, cheeckassignedPrivilege);
                            }
                        }
                    }
                }
                else
                {
                    //移除该用户所有的权限
                    using (DbContext context = new DbContext())
                    {
                        IAssignedPrivilegeRepository iAssignedPrivilegeRepository = RepositoryFactory.Create<IAssignedPrivilegeRepository>();
                        iAssignedPrivilegeRepository.RemoveAssignedPrivilege(context, new Guid(userId));
                    }
                }
                Response.Redirect("UserList.aspx", false);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserList.aspx", false);
        }
    }
}